package com.ntschy.web.controller.bt;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.bt.entity.dataobject.BtHelp.BtHelp;
import com.ntschy.bt.entity.dataobject.BtHelp.SysDept;
import com.ntschy.bt.entity.dataobject.BtSupplyShop;
import com.ntschy.bt.entity.dto.BtHelpDTO.AssignDTO.BtHelpAssignAllocationDTO;
import com.ntschy.bt.entity.dto.BtHelpDTO.AssignDTO.BtHelpAssignPageDTO;
import com.ntschy.bt.entity.enums.BtHelpStatusEnum;
import com.ntschy.bt.entity.mapstruct.BtHelpMapstruct;
import com.ntschy.bt.entity.vo.BtHelp.Assign.BtHelpAssignDetailsVO;
import com.ntschy.bt.entity.vo.BtHelp.Assign.BtHelpAssignPageVo;
import com.ntschy.bt.service.IBtHelp.IBtHelpAssignService;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *
 * </p>
 *
 * @author
 * @since
 */
@Validated
@RestController
@RequestMapping("/bt/help")
public class BtHelpAssignController extends BaseController {

    @Resource
    private IBtHelpAssignService iBtHelpAssignService;



    // * ================================================基础增删改查BEGIN============================================= *
    /**
     * 一键求助分配部门分页列表
     *
     * @param pageDTO
     * @return
     */
    @ApiOperation(value = "查询一键求助列表", notes = "")
    @ApiImplicitParam(name = "pageSupplyShop", value = "分配部门分页查询", required = true, dataType = "BtHelpAssignPageDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @PostMapping("/pageWaitAllotHelp")
    public AjaxPageResult<BtHelpAssignPageVo> pageSupplyShop(@RequestBody BtHelpAssignPageDTO pageDTO) {
        Page<BtHelp> page = PageHelper.startPage(pageDTO.getCurrPage(), pageDTO.getPageSize());
        BtHelp bthelp = BtHelpMapstruct.BT_HELP_ASSIGN_MAP_STRUCT.pagetableDto(pageDTO);
        List<BtHelp> list = iBtHelpAssignService.pageHelpAssign(bthelp);
        List reponelist = new ArrayList();
        list.forEach((item) ->{
            BtHelpAssignPageVo pagepust = BtHelpMapstruct.BT_HELP_ASSIGN_MAP_STRUCT.pagePustrespone(item);
            reponelist.add(pagepust);
        });

        return buildPageResult(reponelist,page);
    }



    /**
     * 一键求助分配部门详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一键求助分配部门详情", notes = "")
    @ApiImplicitParam(name = "fetchWaitAuditHelp", value = "分配部门详情", required = true, dataType = "String")
    @PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @GetMapping("/fetchWaitAllotHelp")
    public AjaxResult<Map> fetchWaitAuditHelp(@NotBlank(message = "ID不能为空") String id){
        BtHelp btHelp = iBtHelpAssignService.detailsHelpAssing(id);
        BtHelpAssignDetailsVO btHelpAssignDetailsVO = BtHelpMapstruct.BT_HELP_ASSIGN_MAP_STRUCT.mainchangeBtHelpAssignDetailsVO(btHelp);
        List<SysDept> selectdept = iBtHelpAssignService.selectdept();
        Map datamap = new HashMap<>();
        datamap.put("btHelpAssignDetailsVO", btHelpAssignDetailsVO);
        datamap.put("selectdept", selectdept);
        return AjaxResult.success("成功",datamap);

    }

    /**
     * 一键求助分配部门
     *
     * @param btHelpAssignAllocationDTO
     * @return
     */
    @ApiOperation(value = "分配部门", notes = "")
    @ApiImplicitParam(name = "updateAllotHelp", value = "分配部门", required = true, dataType = "BtHelpAssignAllocationDTO")
//    @PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @PostMapping("/updateAllotHelp")
    public AjaxResult<String> updateAllotHelp(@RequestBody BtHelpAssignAllocationDTO btHelpAssignAllocationDTO){
        BtHelpAssignAllocationDTO updateAllotHelp = btHelpAssignAllocationDTO;
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datestring = simpleDateFormat.format(date);
        updateAllotHelp.setAssignTime(datestring);
        BtHelp updateBtHelp = BtHelpMapstruct.BT_HELP_ASSIGN_MAP_STRUCT.minBtHelpAssignAllocationDTO(updateAllotHelp);
        String id = updateAllotHelp.getId();
        BtHelp btHelp = iBtHelpAssignService.detailsHelpAssing(id);
        int updatedistribution = 0;
        if(btHelp.getStatus().getValue() == BtHelpStatusEnum.TO_BE_ASSIGNED.getValue()){
            updateBtHelp.setStatus(BtHelpStatusEnum.PENDING_REVIEW);
            updatedistribution = iBtHelpAssignService.updatedistribution(updateBtHelp);
        }
        return  toAjax(updatedistribution,"分配成功","分配失败");
    }



    // * =================================================基础增删改查END============================================== *


    /**
     *
     *分页查询下拉数据
     * @param
     * @return 下拉数据
     */
    @ApiOperation(value = "分页查询下拉数据", notes = "")
    @GetMapping("/pulldown")
    public AjaxResult<Object> pull_down() throws ClassNotFoundException {
        BtHelpAssignPageDTO btHelpAssignPageDTO = new BtHelpAssignPageDTO();
        Map map = iBtHelpAssignService.fingEnum(btHelpAssignPageDTO);
        return  AjaxResult.success("下拉数据",map);
    }




































    // * =================================================基础增删改查END============================================== *

}
